import os
from app.pdf_merger import PDFMerger
from app.pdf_converter import PDFConverter

def get_merger():
    """获取PDF合并器实例"""
    return PDFMerger()

def get_converter():
    """获取PDF转换器实例"""
    return PDFConverter()

def system_cleanup():
    """系统级清理（同时处理合并和转换的临时文件）"""
    for fname in os.listdir('.'):
        if (fname.endswith(('.pdf', '.docx')) 
            and not fname.startswith(('merged', 'converted'))):
            try:
                os.remove(fname)
            except Exception as e:
                print(f"系统清理失败: {e}")

def check_output_dir():
    """检查并创建输出目录"""
    output_dir = os.path.join(os.getcwd(), 'output')
    try:
        os.makedirs(output_dir, exist_ok=True)
        return True
    except Exception as e:
        print(f"无法创建输出目录: {e}")
        return False

def validate_filename(filename, allowed_extensions):
    """验证文件名有效性"""
    if not filename:
        return False
    _, ext = os.path.splitext(filename)
    return ext.lower() in allowed_extensions
